

class TarzanScene extends PhyScene {
    private static _HasInited: boolean
    public static Init() {
        if (TarzanScene._HasInited) {
            return
        }
        TarzanScene._HasInited = true

        core.App.SceneManager.register(SceneConsts.Tarzan, new TarzanScene())
        core.App.ControllerManager.register(ControllerConsts.GameOver, new GameOverControl())
        core.App.ControllerManager.register(ControllerConsts.MainGame, new MainGameControl())
    }

    private _bg: TitleMap
    public get Background(): egret.DisplayObjectContainer {
        return this._bg
    }

    private _lookAtX: number = 0
    private _lookAtY: number = 0
    private _score: number = 0

    //private _ball: Box2D.Dynamics.b2Body = null
    private _ball: PlayerPhyObject = null
    private _randomMap: RandomTerrien = null

    private _gameOver: boolean
    constructor() {
        super()
    }

    public init(): void {
        this._gameOver = false
        this._lookAtX = 0
        this._lookAtY = 0

        console.log("TarzanScene.init" + core.App.StageUtils.getHeight() + " " + core.App.StageUtils.getWidth())
        this._bg = new TitleMap()
        this._bg.initMap()
        LayerManager.Game_Bg.addChild(this._bg)
        this._bg.LookAt(0, 0)

        this.initPhy(this._bg)

        this._ball = new PlayerPhyObject(this.Background, this.World, 15)
        this._randomMap = new RandomTerrien(this, this._ball)

        this.World.SetContactListener(new TarzanContactListener(this))

        {
            /*
            let bodyDef = new Box2D.Dynamics.b2BodyDef()
            bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody
            bodyDef.bullet = true
            bodyDef.linearDamping = 0
            bodyDef.angularDamping = 0
            bodyDef.fixedRotation = false
            bodyDef.allowSleep = true
            bodyDef.position.x = this.PhyLen(-50)
            bodyDef.position.y = this.PhyLen(10) //App.StageUtils.getHeight() / 2 / this._p2m
            bodyDef.angle = Math.PI / 2
            let fixtureDef = new Box2D.Dynamics.b2FixtureDef()
            fixtureDef.density = 1
            fixtureDef.friction = 0
            fixtureDef.restitution = 0.8

            let stone = this._world.CreateBody(bodyDef)
            {
                let shape = new Box2D.Collision.Shapes.b2CircleShape()
                shape.SetRadius(this.PhyLen(20))
                fixtureDef.shape = shape
                stone.CreateFixture(fixtureDef)
            }
            */

            /*
            let joinDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef()
            joinDef.Initialize(stone, this._ball, stone.GetPosition())
            joinDef.enableMotor = true
            this._world.CreateJoint(joinDef)
            */
        }
    }

    private _oldScore: number
    public lookAt(x, y: number) {
        this._bg.LookAt(x, y)
        this._randomMap.LookAt(x, y)

        if (this._oldScore != this._randomMap.Score) {
            this._oldScore = this._randomMap.Score
            core.App.ControllerManager.applyFunc(ControllerConsts.MainGame, MainGameControlEvent.SetScore, this._oldScore)
        }
    }

    private onTap(e: egret.Event) {
        console.log("onTap")
        this._randomMap.onTap()
    }

    private onTapBegin(e: egret.Event) {
        if (this._gameOver == false) {
            this._randomMap.makeTheRole()
        }
    }
    private onTapEnd(e: egret.Event) {
        this._randomMap.releaseTheRole()
    }

    private _boom_sound: egret.Sound
    public GameOver() {
        console.log("GameOver==========================")
        if (this._gameOver == false) {
            this._gameOver = true
            //core.App.ViewManager.open(ui.ViewConst.GameOver)
            if (this._boom_sound == null) {
                this._boom_sound = RES.getRes("boom_mp3");
                this._boom_sound.type = egret.Sound.EFFECT
            }
            this._boom_sound.play(0, 1)
            this._backSoundChannel.stop()
            core.App.ControllerManager.applyFunc(ControllerConsts.GameOver, GameOverControlEvent.GameIsOver, this._randomMap.Score)
        }
    }

    private _backSound: egret.Sound
    private _backSoundChannel: egret.SoundChannel
    public onEnter(): void {
        super.onEnter()

        this.init()

        this.addLayerAt(LayerManager.Game_Bg, 0)
        this.addLayerAt(LayerManager.UI_Game, 1)
        //core.App.ViewManager.open(ui.ViewConst.GameOver)
        core.App.ViewManager.open(ui.ViewConst.MainGame)

        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this)
        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTapBegin, this)
        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_END, this.onTapEnd, this)
        LayerManager.UI_Game.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTapEnd, this)
        LayerManager.UI_Game.touchEnabled = true

        this._backSound = RES.getRes("back_mp3")
        this._backSoundChannel = this._backSound.play()
        this._backSound.type = egret.Sound.MUSIC
    }

    public enterFrame(time: number) {
        if (this._gameOver) {
            return
        }

        this.phyLoop()
        if (this._ball != null) {
            let pos = this._ball.body.GetPosition()
            this.lookAt(PhyScene.EgretLen(pos.x), PhyScene.EgretLen(pos.y))
            this._ball.RefreshPos()
        }
        if (this._randomMap != null) {
            this._randomMap.Refresh()
        }
    }
}